﻿using System;
using static System.Threading.Thread;
using static System.Console;
using System.Threading;

namespace Chapter3.Recipe1
{
    class Program
    {
        private delegate string RunOnThreadPool(out int threadId);
        private static void Callback(IAsyncResult ar)
        {
            WriteLine("Starting a callback...");
            WriteLine($"State passed to a callback: {ar.AsyncState}");
            WriteLine($"Is threadPool thread: {CurrentThread.IsThreadPoolThread}");
            WriteLine($"Thread pool worker thread id: {CurrentThread.ManagedThreadId}");
        }

        private static string Test(out int threadId)
        {
            WriteLine("Starting...");
            WriteLine($"Is thread pool thread: {CurrentThread.IsThreadPoolThread}");
            Sleep(TimeSpan.FromSeconds(2));
            threadId = CurrentThread.ManagedThreadId;
            return $"Thread pool worker thread id was: {threadId}";
        }
        static void Main(string[] args)
        {
            int threadId = 0;
            RunOnThreadPool poolDelegate = Test;
            var t = new Thread(() => Test(out threadId));
            t.Start();
            t.Join();

            WriteLine($"Thread id: {threadId}");

            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback,
                "a delegate asynchronous call");

            string result = poolDelegate.EndInvoke(out threadId, r);

            WriteLine($"Thread pool worker thread id: {threadId}");
            WriteLine(result);
        }
    }
}
